/* Autostop
 *
 * Copyright (c) 2008 Maciej Gajewski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

/* This program duplicates functionality of gpio_example tool 
 * distributed with CMX board, but using libgpio
 */

#include <stdio.h>
#include <string.h>

#include "libgpio.h"

/* ----------------------- main -------------------------- */
int main( int argc, char** argv )
{
	int line;
	struct gpio_state_struct state;
	
	if ( argc != 3 )
	{
		fprintf(stderr, "\nUsage:\t%s <GPIO> <command>\n"
			"\tGPIO    : GPIO number\n"
			"\tcommand : can be one of the following:\n"
			"\t\tSET   - Set GPIO level to high\n"
			"\t\tCLEAR - Set GPIO level to low\n"
			"\t\tREAD  - Read GPIO level\n"
			"\t\tIN    - Set pin to function as input GPIO\n"
			"\t\tOUT   - Set pin to function as output GPIO\n",
			argv[0]);
		return 1;
	}
	
	/* parsing line number */
	if(sscanf(argv[1], "%d", &line) != 1){
		fprintf(stderr, "%s: '%s' is not correct GPIO line number\n", argv[0], argv[1]);
		return 2;
	}
	
	/* init */
	gpio_init();
	state.function	= LG_FN_DONTCARE;
	state.direction	= LG_GPIO_DONTCARE;
	state.state		= LG_GPIO_DONTCARE;
	
	if ( strcmp( argv[2], "OUT" )  == 0 )
	{
		state.function = LG_FN_GPIO;
		state.direction = LG_GPIO_OUT;
		gpio_set( line, & state );
	}
	else if ( strcmp( argv[2], "IN" )  == 0 )
	{
		state.function = LG_FN_GPIO;
		state.direction = LG_GPIO_IN;
		gpio_set( line, & state );
	}
	else if ( strcmp( argv[2], "SET" )  == 0 )
	{
		state.state = 1;
		gpio_set( line, & state );
	}
	else if ( strcmp( argv[2], "CLEAR" )  == 0 )
	{
		state.state = 0;
		gpio_set( line, & state );
	}
	else if ( strcmp( argv[2], "READ" )  == 0 )
	{
		fprintf( stderr, "READ not implemented\n" );
		return 3;
	}
	else
	{
		fprintf( stderr, "Unknown operation '%s'\n", argv[2] );
		return 4;
	}
	
	return 0;

}


/* eof */

